package xyz.icxl.kotlion.ext.dsl.span

import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.view.View
import android.widget.TextView
import androidx.annotation.Px
import xyz.icxl.kotlion.ext.dsl.span.impl.DslSpannableStringBuilderImpl

/**
 * 文件描述：TextView Span扩展
 *
 * 文件创建人：cxl
 *
 * 创建时间：2022 年 03月 24 日
 *
 * 文件版本：v1.0
 *
 * 版本描述：创建文件
 */

interface DslSpannableStringBuilder {
    //增加一段文字
    fun addText(text: String, method: (DslSpanBuilder.() -> Unit)? = null)
}

interface DslSpanBuilder {
    //设置文字颜色
    fun setColor(color: String)
    //default,default-bold,monospace,serif,sans-serif
    fun setTypeface(typeFace:String)
    fun setTextSize(@Px size:Int)
    //设置点击事件
    fun onClick(useUnderLine: Boolean = true, onClick: (View) -> Unit)
}

//为 TextView 创建扩展函数，其参数为接口的扩展函数
fun TextView.buildSpannableString(init: DslSpannableStringBuilder.() -> Unit) {
    //具体实现类
    val spanStringBuilderImpl = DslSpannableStringBuilderImpl()
    spanStringBuilderImpl.init()
    movementMethod = LinkMovementMethod.getInstance()
    //通过实现类返回SpannableStringBuilder
    text = spanStringBuilderImpl.build()
}

fun createSpannableString(init: DslSpannableStringBuilder.() -> Unit): SpannableStringBuilder {
    //具体实现类
    val spanStringBuilderImpl = DslSpannableStringBuilderImpl()
    spanStringBuilderImpl.init()
    return spanStringBuilderImpl.build()
}